/** 子模块接口，为组件提供数据操作 */
import { Injectable } from '@angular/core';

import { Home } from './home';

const HOMES = [
  new Home(11, '条目 1', '条目 11', '/assets/img/angular.png'),
  new Home(12, '条目 2', '条目 12', '/assets/img/angular.png'),
  new Home(13, '条目 3', '条目 13', '/assets/img/angular.png')
];
const homesPromise = Promise.resolve(HOMES);

@Injectable()
export class HomeService {
  public static nextHomeId = 100;

  public getHomeList() { return homesPromise; }

  public getHome(id: number | string) {
    return homesPromise
      .then((homes) => homes.find((home) => home.id === +id));
  }

  public addHome(name: string) {
    name = name.trim();
    if (name) {
      const homes = new Home(HomeService.nextHomeId++, name);
      homesPromise.then((homeList) => homeList.push(homes));
    }
  }
}
